package com.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Category;
import com.itheima.bean.PageParam;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.SetMealDao;
import com.itheima.dao.SetMealDishDao;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.CustomException;
import com.zhao.dao.SetmealDao;
import com.zhao.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service

public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetMealDao dao;
    @Autowired
    private SetMealDishDao sd;
    @Autowired
    private CategoryDao categoryDao;


    @Override
    public long findCategoryCountById(long cid) {
        LambdaQueryWrapper<com.itheima.bean.Setmeal> qw = new LambdaQueryWrapper<com.itheima.bean.Setmeal>();
        qw.eq(com.itheima.bean.Setmeal::getCategoryId, cid);

        return dao.selectCount(qw);
    }

    @Override
    public int add(SetmealDto setmealDto) {
//        添加套餐的基本信息
        int row = dao.insert(setmealDto);
        int count = 0;
        System.out.println("===================== = " + setmealDto.getCategoryId());
//        获取菜品表到集合
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : list) {
//            设置这个菜品添加到表里面去的时候 id表示关联的是哪一个套餐
            setmealDish.setSetmealId(setmealDto.getId());

            count += sd.insert(setmealDish);

        }


        return row > 0 && count == list.size() ? 1 : 0;
    }

    @Override
    public IPage<SetmealDto> findPage(PageParam pageParam) {
//        先查询套餐表
        Page<com.itheima.bean.Setmeal> p = new Page<>(pageParam.getPage(), pageParam.getPageSize());
//        1 设置查询条件
        LambdaQueryWrapper<com.itheima.bean.Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(pageParam.getName() != null, com.itheima.bean.Setmeal::getName, pageParam.getName());
//         2 万事俱备 查套餐
        IPage<com.itheima.bean.Setmeal> setmealIPage = dao.selectPage(p, qw);

//        3封装记录和recors
        IPage<SetmealDto> setmealDtoIPage = new Page<>();
//        封装tatol
        setmealDtoIPage.setTotal(setmealIPage.getTotal());
//        封装recors
        List<SetmealDto> setmealDtolist = new ArrayList<>();
        for (com.itheima.bean.Setmeal setmeal : setmealIPage.getRecords()) {
//            有了新对象
            SetmealDto setmealDto = new SetmealDto();
//            把新对象幅值给dto
            BeanUtils.copyProperties(setmeal, setmealDto);
//       还要查套餐分类的名字
            Category category = categoryDao.selectById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());


            setmealDtolist.add(setmealDto);

        }


        setmealDtoIPage.setRecords(setmealDtolist);


        return setmealDtoIPage;
    }

    @Override
    public int delete(List<Long> ids) {
//        遍历集合 取出id
        for (Long id : ids) {
//            查询套餐
            com.itheima.bean.Setmeal setmeal = dao.selectById(id);
//            判断
            if (setmeal.getStatus() == 1) {
                throw new CustomException("当前套餐处于起售状态 禁止删除");
            }

        }
//        要么就删除
        int row = dao.deleteBatchIds(ids);
//      删除 setmeal和dish 的关联信息；
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();

        qw.in(SetmealDish::getSetmealId, ids);

        int row2 = sd.delete(qw);

        return row > 0 && row2 > 0 ? 1 : 0;
    }

    @Override
    public List<com.itheima.bean.Setmeal> findCotegoryId(Long categoryId, int status) {
        LambdaQueryWrapper<com.itheima.bean.Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(com.itheima.bean.Setmeal::getStatus, status);
        qw.eq(com.itheima.bean.Setmeal::getCategoryId, categoryId);
        return dao.selectList(qw);
    }

    @Override
    public int updateStatus(List<Long> ids) {
        com.itheima.bean.Setmeal setmeal = new com.itheima.bean.Setmeal();
        setmeal.setStatus(0);
        for (Long id : ids) {
            setmeal.setId(id);
        }

        int row = dao.updateById(setmeal);
        return row;
    }


    @Override
    public int updateStatus1(List<Long> ids) {
        com.itheima.bean.Setmeal setmeal = new com.itheima.bean.Setmeal();
        setmeal.setStatus(1);
        for (Long id : ids) {
            setmeal.setId(id);
        }

        int row = dao.updateById(setmeal);
        return row;
    }
    //修改套餐
    @Override
    public int update(SetmealDto setmealDto) {
//        修改套餐的基本数据
        com.itheima.bean.Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        int row = dao.updateById(setmeal);
//        修改的套餐装到dto身上
        int count = 0;
        if (row > 0) {
            LambdaQueryWrapper<SetmealDish> smd = new LambdaQueryWrapper<>();
//            看一下这个套餐里面是否有这个菜品
            smd.eq(SetmealDish::getDishId, setmeal.getId());
//            因为要重置 所已删除
            sd.delete(smd);

            List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
//判断 有没有这道菜
            if (setmealDishList != null && setmealDishList.size() > 0) {

                for (SetmealDish dish : setmealDishList) {
                    dish.setSetmealId(setmealDto.getId());
                    count += sd.insert(dish);

                }
            }


        }
        return (row > 0 && count == setmealDto.getSetmealDishes().size() ? 1 : 0);
    }

}
